package nc.bs.pu.mpp.strategy.mrbill;

import nc.impl.pubapp.pattern.database.DataAccessUtils;
import nc.vo.bd.material.MaterialVO;
import nc.vo.pu.pub.enumeration.POEnumBillStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.data.IRowSet;
import nc.vo.scmpub.tbb.TbbTempTableSqlBuilder;
import nc.vo.tb.obj.NtbParamVO;

public class MrBillBatchLinkQuery extends MrBillBatchFetcher{

	public String[] readyLinkQuery422XForTbb(NtbParamVO vo) {
		TbbTempTableSqlBuilder tbbuilder = new TbbTempTableSqlBuilder();
		String sql = this.getReadyQuerySql(vo, new NtbParamVO[]{vo}, (String) null, tbbuilder);
		IRowSet rowset = (new DataAccessUtils()).query(sql);
		String[] bids = rowset.toOneDimensionStringArray();
		return bids;
	}

	protected String getReadyQuerySql(NtbParamVO seed, NtbParamVO[] params, String type,
			TbbTempTableSqlBuilder tbbuilder) {
		tbbuilder.buildSql(this.getFieldMap(seed), params);
		StringBuilder select = new StringBuilder();
		select.append("select distinct h.pk_storereq ");
		StringBuilder from = new StringBuilder();
		from.append(" from po_storereq_b b inner join po_storereq h on ");
		from.append(" b.pk_storereq = h.pk_storereq ");
		if (this.isIncludeField(seed, "MATCLASS")) {
			from.append(" left outer join " + MaterialVO.getDefaultTableName() + " inv on  ");
			from.append(" b.pk_material=inv.pk_material ");
		}

		from.append(tbbuilder.getFrompart());
		StringBuilder where = new StringBuilder();
		where.append(" where h.dr = 0 and  b.dr = 0 ");
		where.append(" and b.bclose = '" + UFBoolean.FALSE.toString() + "'");
		where.append(" and h.fbillstatus = " + POEnumBillStatus.APPROVE.toInt());
		where.append(tbbuilder.getWherepart(true));
		return select.append(from.toString()).append(where.toString()).toString();
	}
}
